package com.lee.dutylower.view.wiget;

import android.app.AlertDialog;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.lee.dutylower.R;
import com.lee.dutylower.activity.FlashActivity;
import com.lee.dutylower.utils.DeviceUtil;
import com.lee.dutylower.utils.SharedPreferencesUtils;
import com.lee.dutylower.utils.ToastUtils;

import net.margaritov.preference.colorpicker.ColorPickerDialog;

/**
 * Created by lijie on 2017/7/20.
 */

public class FlashSettingView extends RelativeLayout implements ColorPickerDialog.OnColorChangedListener  {
    FlashActivity context;
    EditText etLe;
    EditText etRi;
    EditText etSi;
    Button btnChooseColor;
    Button btnShowColor;
    ImageView ivOK;
    ImageView ivCancel;
    int color;
    public FlashSettingView(Context context) {
        super(context);
    }

    public FlashSettingView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FlashSettingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    public FlashSettingView(AlertDialog dialog, FlashActivity context, String leftContent, String rightContent, int tvSize, int tvColor) {
        super(context);
        this.context = context;
        this.color = tvColor;
        this.addView(initView(dialog,context,leftContent,rightContent,tvSize,tvColor));
    }

    @Override
    public void onColorChanged(int color) {
        this.color = color;
        btnShowColor.setBackgroundColor(color);
    }
    private View initView(final AlertDialog dialog, final FlashActivity context, String leftContent, String rightContent, int tvSize, final int tvColor){
        View v = LayoutInflater.from(context).inflate(R.layout.ll_setting_flash,null);

        etLe = v.findViewById(R.id.et_flash_left_content);
        etRi = v.findViewById(R.id.et_flash_right_content);
        etSi = v.findViewById(R.id.et_size);
        btnChooseColor = v.findViewById(R.id.btn_color_choose);
        btnShowColor = v.findViewById(R.id.btn_color_show);
        ivOK = v.findViewById(R.id.iv_ok);
        ivCancel = v.findViewById(R.id.iv_cancel);

        etLe.setText(leftContent);
        etRi.setText(rightContent);
        etSi.setText(tvSize+"");
        btnShowColor.setBackgroundColor(tvColor);
        btnChooseColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ColorPickerDialog colorPickerDialog = new ColorPickerDialog(context, tvColor);
                colorPickerDialog.setOnColorChangedListener(FlashSettingView.this);
                colorPickerDialog.show();
            }
        });
        ivOK.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferencesUtils.setParam(context,"tv-content-left",etLe.getText().toString());
                SharedPreferencesUtils.setParam(context,"tv-content-right",etRi.getText().toString());
                SharedPreferencesUtils.setParam(context,"tv-size",Integer.parseInt(etSi.getText().toString()));
                SharedPreferencesUtils.setParam(context,"tv-color",color);
                ToastUtils.showLong(context,"保存成功");
                context.updateFlash();
                dialog.cancel();
            }
        });
        ivCancel.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.cancel();
            }
        });
        return v;
    }
}
